/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.batavia.inter.impl;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
import com.gargoylesoftware.htmlunit.util.Cookie;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.List;
import org.batavia.domain.User;
import org.batavia.inter.InterfaceLogin;
import org.joda.time.DateTime;

/**
 *
 * @author omenk
 */
public class LoginImpl implements InterfaceLogin {

    private User user;
    //private User user;

    public User login(String username, String password) {
        try {
            WebClient client = new WebClient(BrowserVersion.getDefault());

            client.setJavaScriptEnabled(false);
            client.setThrowExceptionOnScriptError(true);
            client.setCssEnabled(false);

            HtmlPage page = client.getPage("http://web.batavia-air.com/MyPage/login.php");
            HtmlForm form = page.getFormByName("loginForm");

            form.getInputByName("useridmp").setValueAttribute(username);
            form.getInputByName("passwordmp").setValueAttribute(password);

            HtmlSubmitInput button = form.getInputByValue("sign in!");

            final HtmlPage page2 = button.click();

            // AirlinesImpl air = new AirlinesImpl();

            HtmlForm form2 = page2.getFormByName("searchbook");

            //form2.getCheckedRadioButton("radio").setAttribute("radio", "flight");
            //form2.getRadioButtonsByName("radio").get(1).setAttribute("flight","flight");


            // DateTime berangkat = new DateTime(2011, 6, 6, 0, 0, 0, 0);
            //air.search(berangkat.toDate(), "CGK", "JOG", 1, 0, 0);


            form2.getSelectByName("ruteBerangkat").setSelectedAttribute("CGK", true);
            form2.getSelectByName("ruteTujuan").setSelectedAttribute("JOG", true);

            DateTime depart = new DateTime(2011, 12, 20, 0, 0, 0, 0);

            form2.getSelectByName("tglBerangkatPergi").setSelectedAttribute(depart.getDayOfMonth() + "-", true);
            form2.getSelectByName("blnBerangkatPergi").setSelectedAttribute(depart.getMonthOfYear() + "-", true);
            form2.getSelectByName("thnBerangkatPergi").setSelectedAttribute(depart.getYear() + "", true);


            form2.getSelectByName("jmlPenumpang").setSelectedAttribute("2", true);
            form2.getSelectByName("jmlInfant").setSelectedAttribute("1", true);

            final HtmlSubmitInput btn = form2.getInputByName("Submit");
            final HtmlPage page3 = btn.click();
            // logger.debug("Page 2 : " + page2.getTitleText());

           // System.out.println(page3.asText());
            System.out.println(page3.getUrl());


            List<HtmlElement> flatNo = (List<HtmlElement>) page2.getByXPath(BataConstants.XPATH_FLTNO);
            List<HtmlElement> dep = (List<HtmlElement>) page2.getByXPath(BataConstants.XPATH_DEP);
            List<HtmlElement> arr = (List<HtmlElement>) page2.getByXPath(BataConstants.XPATH_ARR);
            List<HtmlElement> via = (List<HtmlElement>) page2.getByXPath(BataConstants.XPATH_VIA);

            List<HtmlElement> z = (List<HtmlElement>) page2.getByXPath(BataConstants.XPATH_Z);

            List<HtmlElement> w = (List<HtmlElement>) page2.getByXPath(BataConstants.XPATH_W);
            List<HtmlElement> p = (List<HtmlElement>) page2.getByXPath(BataConstants.XPATH_P);
            List<HtmlElement> n = (List<HtmlElement>) page2.getByXPath(BataConstants.XPATH_N);
            List<HtmlElement> x = (List<HtmlElement>) page2.getByXPath(BataConstants.XPATH_X);
            List<HtmlElement> r = (List<HtmlElement>) page2.getByXPath(BataConstants.XPATH_R);
            List<HtmlElement> s = (List<HtmlElement>) page2.getByXPath(BataConstants.XPATH_S);
            List<HtmlElement> t = (List<HtmlElement>) page2.getByXPath(BataConstants.XPATH_T);
            List<HtmlElement> v = (List<HtmlElement>) page2.getByXPath(BataConstants.XPATH_V);
            List<HtmlElement> q = (List<HtmlElement>) page2.getByXPath(BataConstants.XPATH_Q);
            List<HtmlElement> b = (List<HtmlElement>) page2.getByXPath(BataConstants.XPATH_B);
            List<HtmlElement> l = (List<HtmlElement>) page2.getByXPath(BataConstants.XPATH_L);
            List<HtmlElement> m = (List<HtmlElement>) page2.getByXPath(BataConstants.XPATH_N);
            List<HtmlElement> h = (List<HtmlElement>) page2.getByXPath(BataConstants.XPATH_H);
            List<HtmlElement> d = (List<HtmlElement>) page2.getByXPath(BataConstants.XPATH_D);
            List<HtmlElement> y = (List<HtmlElement>) page2.getByXPath(BataConstants.XPATH_Y);



            //DateTime expire = new DateTime().plusDays(30);

            //client.getCookieManager().addCookie(new Cookie("http://web.batavia-air.com/MyPage/index.php", "PHPSESSID", "fd39d3902fc59e9603907b6d81efc60d", "/", expire.toDate(), false));
            // logger.debug("Page 2 : " + page2.getTitleText());


            // System.out.println(page2.asText());


            //HtmlForm f3 = page3.getFormByName("pilihPenerbangan");
            //HtmlForm f3 = page3.getFormByName("searchbook");


            // f3.getInputByName("");




            client.closeAllWindows();


            //DateTime expire = new DateTime().plusDays(30);

            //client.getCookieManager().addCookie(new Cookie("http://web.batavia-air.com/MyPage/index.php", "PHPSESSID", "fd39d3902fc59e9603907b6d81efc60d", "/", expire.toDate(), false));
            //HtmlPage page3 = client.getPage("http://web.batavia-air.com/MyPage/index.php");
            //HtmlForm form3 = page.getFormByName("searchbook");


        } catch (MalformedURLException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        return user;

    }
}
